/**
 * @Description:
 * @Author bianpengfei
 * @create 2022/1/16 19:24
 */

/**
 * 格式化时间戳间距  2日24时33分20秒
 * @param {Number} a
 * @param {Number} b
 * @returns {string}
 */
export function diffTimeFormat(a, b = new Date().valueOf()) {
  let diff = b - a
  let s = 1000
  let m = 60 * s
  let h = 60 * m
  let d = 24 * h

  let day = ~~(diff / d)
  let hour = ~~((diff % d) / h)
  let min = ~~(((diff % d) % h) / m)
  let second = ~~((((diff % d) % h) % m) / s)

  return [
    day && day + '日',
    hour && hour + '时',
    min && String(min).padStart(2, '0') + '分',
    second && String(second).padStart(2, '0') + '秒'
  ]
    .filter(Boolean)
    .join('')
}

/**
 * 动态计算table 高度
 * @param {HTMLElement} el
 * @param {Object} opts
 * @returns {String}
 */
export function computedElTableHeight(el, opts = {}) {
  opts = {
    ...{ offsetBottom: 100 },
    ...opts
  }
  el ||= document.querySelector('.el-table')
  const tableRect = el.getBoundingClientRect()
  console.log('tableRect', tableRect, el)
  const clientHeight = document.documentElement.clientHeight || document.body.clientHeight
  // return clientHeight - tableRect.top - opts.offsetBottom
  return `calc(100vh - ${tableRect.top}px - ${opts.offsetBottom}px)`
}

/**
 * 鉴定是否是合法的URL
 * @param textval
 * @returns {boolean}
 */
export function validateURL(textval) {
  const urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return urlregex.test(textval)
}

/**
 * 是否是外部的link
 * @param routePath
 * @returns {boolean}
 */
export function isExternalLink(routePath) {
  return validateURL(routePath)
}
